package com.addplus.townmall.server.api.mapper.town;

import com.addplus.townmall.server.api.model.town.ShopDiscountZone;
import com.addplus.townmall.server.api.model.town.expand.ShopDiscountZoneExpand;
import com.addplus.townmall.server.api.utils.BaseAddMapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 商城折扣专区 Mapper 接口
 * </p>
 *
 * @author zhangjiehang
 * @since 2018-08-27
 */
public interface ShopDiscountZoneMapper extends BaseAddMapper<ShopDiscountZone> {

    @Select("select s.store_name,sp.sale_virtual_count,spd.shop_comment_count,sdz.* " +
            "from shop_discount_zone  sdz " +
            "inner JOIN store  s ON s.id = sdz.store_id " +
            "inner JOIN shop_product sp ON sdz.shop_product_id = sp.id " +
            "inner JOIN shop_product_describe spd ON sp.id = spd.shop_product_id " +
            "where ${ew.sqlSegment}")
    List<ShopDiscountZoneExpand> getShopDiscountZoneByQry(Page page, @Param("ew") Wrapper wrapper);

    @Select("<script>\n" +
            "SELECT\n" +
            " a.id,\n" +
            " a.store_id,\n" +
            " s.store_name ,\n" +
            " a.shop_product_id,\n" +
            " a.shop_sku_id,\n" +
            " a.shop_name,\n" +
            " a.shop_describe,\n" +
            " a.shop_seal_img,\n" +
            " a.line_price,\n" +
            " a.sale_price,\n" +
            " a.discount_rate,\n" +
            " d.good_rate,\n" +
            " d.total_comment\n" +
            "FROM\n" +
            " shop_discount_zone a\n" +
            " LEFT JOIN store s ON s.id = a.store_id AND s.is_delete = 0\n" +
            " LEFT JOIN shop_product p ON p.id = a.shop_product_id AND p.is_deleted = 0 \n" +
            " LEFT JOIN shop_comment_all d ON d.shop_product_id = a.shop_product_id AND d.is_deleted = 0\n" +
            " WHERE \n" +
            " <if test=\"categoryList!= null\">\n" +
            " p.shop_category_id in \n" +
            "  <foreach collection='categoryList' open='('  item='item'  separator=',' close=')'> #{item}</foreach>\n" +
            " AND\n" +
            " </if>\n" +
            " a.is_delete = 0\n" +
            " AND a.online_status = 1\n" +
            " ORDER BY a.sort\n" +
            "</script>")
    List<ShopDiscountZoneExpand> getShopDiscountZoneByCategorey(Page page, @Param("categoryList") List<Integer> categoryList);

    @Select("select s.store_name,a.* " +
            "from shop_discount_zone  a " +
            "LEFT JOIN store  s ON s.id = a.store_id where a.is_delete=0 order by a.gmt_modified desc")
    List<ShopDiscountZoneExpand> getAllShopDiscountZone(IPage page);
    @Select("select IFNULL(max(sort)+1,1) as sort from shop_discount_zone where is_delete=0")
    Integer maxSort();


    @Select("select s.store_name,sp.shop_tag,sku.sku_describe,sku.sale_price as sku_sale_price,sku.line_price as sku_line_price,sku.stock,sku.limit_num,sdz.* " +
            "from shop_discount_zone  sdz " +
            "LEFT JOIN store  s ON s.id = sdz.store_id LEFT JOIN shop_product sp ON sdz.shop_product_id = sp.id LEFT JOIN shop_specification_sku sku ON sdz.shop_sku_id = sku.id where sdz.is_delete=0 AND sdz.online_status=1 and sdz.shop_product_id=#{shopId}")
    List<ShopDiscountZoneExpand> getShopDiscountZoneByShopId(@Param("shopId") Integer shopId);

    @Select("select sku.sku_describe,sku.sale_price as sku_sale_price,sku.line_price as sku_line_price,sku.stock,sku.limit_num,sdz.shop_sku_id " +
            "from shop_discount_zone  sdz " +
            "LEFT JOIN shop_specification_sku sku ON sdz.shop_sku_id = sku.id where sdz.is_delete=0 and sdz.shop_product_id=#{shopId}")
    List<ShopDiscountZoneExpand> getSkuList(@Param("shopId") Integer shopId);
}
